package edu.wuster.blog.service.impl;

import edu.wuster.blog.entity.Blogpart;
import edu.wuster.blog.mapper.BlogpartMapper;
import edu.wuster.blog.mapper.SearchMapper;
import edu.wuster.blog.service.SearchService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDate;
import java.util.List;
import java.util.Iterator;


@Slf4j
@Service
public class SearchServiceImpl implements SearchService {

    @Autowired
    private SearchMapper searchMapper;
    @Autowired
    private BlogpartMapper blogpartMapper;

    //用户查看个人所有博客
    @Override
    public List<Blogpart> selectByAuthor(String author){
        log.info("查看用户（{}）的博客", author);

        List<Blogpart> list = searchMapper.selectByAuthor(author);
        //剔除查询到的博客列表中已经被删除的博客
        Iterator<Blogpart> iterator = list.iterator();
        while(iterator.hasNext()){
            Blogpart blogpart = iterator.next();
            if (blogpart.getIsDelete() == 1){
                iterator.remove();
            }
        }
        log.info("已筛选出用户已删除的博客");
        return list;

    }

    //根据标签搜索博客,限制状态“已发布”
    @Override
    public List<Blogpart> selectByLabel(String name){
        log.info("查看带有标签（{}）的博客", name);

        List<Blogpart> list = searchMapper.selectByLabel(name);
        //剔除查询到的博客列表中已经被删除的博客
        Iterator<Blogpart> iterator = list.iterator();
        while(iterator.hasNext()){
            Blogpart blogpart = iterator.next();
            if (blogpart.getIsDelete() == 1 || !blogpart.getState().equals("已发布") ){
                iterator.remove();
            }
        }

        log.info("已筛选出标签下已被删除的博客");
        return list;
    }

    //用户在个人博客范围内模糊搜索
    @Override
    public List<Blogpart> userSelectMohu(String mohu,String author){


        log.info("用户在个人博客范围内模糊搜索");

        List<Blogpart> list = searchMapper.userSelectMohu(mohu,author);

        //剔除查询到的博客列表中已经被删除的博客
        Iterator<Blogpart> iterator = list.iterator();
        while(iterator.hasNext()){
            Blogpart blogpart = iterator.next();
            if (blogpart.getIsDelete() == 1){
                iterator.remove();
            }
        }

        log.info("已筛选出列表中已被删除的博客");
        return list;

    }

    @Override
    public List<Blogpart> selectByAdmin(String title, String state, LocalDate begin, LocalDate end) {

        //设置分页大小，第几页，一页多少个
        //PageHelper.startPage(page, pageSize);

        //获取查询结果
        List<Blogpart> list = searchMapper.selectByAdmin(title,state,begin,end);

        //将结果集合强转成Page
        //Page<Blogpart> p = (Page<Blogpart>) list;

        //返回分页结果
        return list;
    }

    //首页根据标题模糊搜索,限制状态“已发布”
    @Override
    public List<Blogpart> homeSelectMohu(String title, Integer likeNum, LocalDate begin, LocalDate end) {

        //设置分页大小，第几页，一页多少个
        //PageHelper.startPage(page, pageSize);

        //获取查询结果
        List<Blogpart> list = searchMapper.homeSelectMohu(title,likeNum,begin,end);

        //将结果集合强转成Page
        //Page<Blogpart> p = (Page<Blogpart>) list;

        //返回分页结果
        return list;
    }
}
